package edu.gatech.teamrocket;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.dao.Dao;

import edu.gatech.teamrocket.models.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditCategoryActivity extends Activity implements OnClickListener, TextWatcher {
	DewApplication app;
	Auth auth;
	
	private Category category;
	
	private EditText categoryName;
	private Button addButton;
	private Button cancelButton;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = (DewApplication) getApplicationContext();
		auth = app.getAuth();
		
		Bundle extras = getIntent().getExtras();
		int categoryID = extras.getInt("CATEGORY_ID");
		
		try {
			DatabaseHelper db = app.getHelper();
			category = db.getCategoryDao().queryForId(categoryID);
		} catch (Exception e) {
			throw new RuntimeException();
		}
		
		setContentView(R.layout.newcategory);
		
		// get all the form elements
		categoryName = (EditText) findViewById(R.id.categoryName);
		addButton = (Button) findViewById(R.id.addButton);
		cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(this);
		addButton.setOnClickListener(this);
		addButton.setText("Save Category");
		categoryName.setText(category.toString());
		categoryName.addTextChangedListener(this);
	}
	
	@Override
	public void onClick(View v) {
		Intent i;
		switch (v.getId()) {
		case R.id.cancelButton: // has the cancel button been pressed?
			Toast.makeText(this, "Maybe another time!", Toast.LENGTH_LONG).show();
			i = new Intent(getApplicationContext(), CategoryListActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // remove this activity from history
			startActivity(i);	
			finish();
			break;
		case R.id.addButton:		
			String name = categoryName.getText().toString();
			String oldName = category.getName();
			category.setName(name);

			// save the category to the database
			try {
				DatabaseHelper db = app.getHelper();
				Dao<Category, Integer> categoryDao = db.getCategoryDao();
				List<Category> categories = categoryDao.queryBuilder().where().eq("user_id", auth.getUser()).and().eq("name", name).query();
				if (categories.size() == 0 || name.equals(oldName)) {
					categoryDao.update(category);
					Toast.makeText(this, "The category was edited successfully!", Toast.LENGTH_LONG).show();
					i = new Intent(getApplicationContext(), CategoryListActivity.class);
					i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // remove this activity from history
		    		startActivity(i);
		    		finish();
				} else {
					Toast.makeText(this, "This category already exists.", Toast.LENGTH_LONG).show();
				}
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}			
    		break;
		}
	}
	
	@Override
	public void afterTextChanged(Editable s) {
		String name = categoryName.getText().toString();
		
		if (name.length() > 0) { // is the category name filled in?
			addButton.setEnabled(true);
		} else {
			addButton.setEnabled(false);
		}
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) { }
	
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) { }
}
